home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zmisc2.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.8 KB  |  287 lines

  1. /* Copyright (C) 1992, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zmisc2.c,v 1.3 2000/09/19 19:00:54 lpd Exp $ */
  20. /* Miscellaneous Level 2 operators */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "ghost.h"
  24. #include "oper.h"
  25. #include "estack.h"
  26. #include "iddict.h"
  27. #include "idparam.h"
  28. #include "iparam.h"
  29. #include "dstack.h"
  30. #include "ilevel.h"
  31. #include "iname.h"
  32. #include "iutil2.h"
  33. #include "ivmspace.h"
  34. #include "store.h"
  35.  
  36. /* Forward references */
  37. private int set_language_level(P2(i_ctx_t *, int));
  38.  
  39. /* ------ Language level operators ------ */
  40.  
  41. /* - .languagelevel <int> */
  42. private int
  43. zlanguagelevel(i_ctx_t *i_ctx_p)
  44. {
  45.     os_ptr op = osp;
  46.  
  47.     push(1);
  48.     make_int(op, LANGUAGE_LEVEL);
  49.     return 0;
  50. }
  51.  
  52. /* <int> .setlanguagelevel - */
  53. private int
  54. zsetlanguagelevel(i_ctx_t *i_ctx_p)
  55. {
  56.     os_ptr op = osp;
  57.     int code = 0;
  58.  
  59.     check_type(*op, t_integer);
  60.     if (op->value.intval != LANGUAGE_LEVEL) {
  61.     code = set_language_level(i_ctx_p, (int)op->value.intval);
  62.     if (code < 0)
  63.         return code;
  64.     }
  65.     LANGUAGE_LEVEL = op->value.intval;
  66.     pop(1);
  67.     return code;
  68. }
  69.  
  70. /* ------ Initialization procedure ------ */
  71.  
  72. /* The level setting ops are recognized even in Level 1 mode. */
  73. const op_def zmisc2_op_defs[] =
  74. {
  75.     {"0.languagelevel", zlanguagelevel},
  76.     {"1.setlanguagelevel", zsetlanguagelevel},
  77.     op_def_end(0)
  78. };
  79.  
  80. /* ------ Internal procedures ------ */
  81.  
  82. /*
  83.  * Adjust the interpreter for a change in language level.
  84.  * This is used for the .setlanguagelevel operator,
  85.  * and (perhaps someday) after a restore.
  86.  */
  87. private int swap_level_dict(P2(i_ctx_t *i_ctx_p, const char *dict_name));
  88. private int swap_entry(P4(i_ctx_t *i_ctx_p, ref elt[2], ref * pdict,
  89.               ref * pdict2));
  90. private int
  91. set_language_level(i_ctx_t *i_ctx_p, int new_level)
  92. {
  93.     int old_level = LANGUAGE_LEVEL;
  94.     ref *pgdict =        /* globaldict, if present */
  95.     ref_stack_index(&d_stack, ref_stack_count(&d_stack) - 2);
  96.     ref *level2dict;
  97.     int code = 0;
  98.  
  99.     if (new_level < 1 ||
  100.     new_level >
  101.     (dict_find_string(systemdict, "ll3dict", &level2dict) > 0 ? 3 : 2)
  102.     )
  103.     return_error(e_rangecheck);
  104.     if (dict_find_string(systemdict, "level2dict", &level2dict) <= 0)
  105.     return_error(e_undefined);
  106.     /*
  107.      * As noted in dstack.h, we allocate the extra d-stack entry for
  108.      * globaldict even in Level 1 mode; in Level 1 mode, this entry
  109.      * holds an extra copy of systemdict, and [count]dictstack omit the
  110.      * very bottommost entry.
  111.      */
  112.     while (new_level != old_level) {
  113.     switch (old_level) {
  114.         case 1: {        /* 1 => 2 or 3 */
  115.                 /* Put globaldict in the dictionary stack. */
  116.         ref *pdict;
  117.  
  118.         /*
  119.          * This might be called so early in initialization that
  120.          * globaldict hasn't been defined yet.  If so, just skip
  121.          * this step.
  122.          */
  123.         code = dict_find_string(level2dict, "globaldict", &pdict);
  124.         if (code > 0) {
  125.             if (!r_has_type(pdict, t_dictionary))
  126.             return_error(e_typecheck);
  127.             *pgdict = *pdict;
  128.         }
  129.         /* Set other flags for Level 2 operation. */
  130.         dict_auto_expand = true;
  131.         }
  132.         code = swap_level_dict(i_ctx_p, "level2dict");
  133.         if (code < 0)
  134.             return code;
  135.         ++old_level;
  136.         continue;
  137.         case 3:        /* 3 => 1 or 2 */
  138.         code = swap_level_dict(i_ctx_p, "ll3dict");
  139.         if (code < 0)
  140.             return code;
  141.         --old_level;
  142.         continue;
  143.         default:        /* 2 => 1 or 3 */
  144.         break;
  145.     }
  146.     switch (new_level) {
  147.         case 1: {        /* 2 => 1 */
  148.         /*
  149.          * Clear the cached definition pointers of all names defined
  150.          * in globaldict.  This will slow down future lookups, but
  151.          * we don't care.
  152.          */
  153.         int index = dict_first(pgdict);
  154.         ref elt[2];
  155.  
  156.         while ((index = dict_next(pgdict, index, &elt[0])) >= 0)
  157.             if (r_has_type(&elt[0], t_name))
  158.             name_invalidate_value_cache(&elt[0]);
  159.         /* Overwrite globaldict in the dictionary stack. */
  160.         *pgdict = *systemdict;
  161.         /* Set other flags for Level 1 operation. */
  162.         dict_auto_expand = false;
  163.         }
  164.         code = swap_level_dict(i_ctx_p, "level2dict");
  165.         break;
  166.         case 3:        /* 2 => 3 */
  167.         code = swap_level_dict(i_ctx_p, "ll3dict");
  168.         break;
  169.         default:        /* not possible */
  170.         return_error(e_Fatal);
  171.     }
  172.     break;
  173.     }
  174.     dict_set_top();        /* reload dict stack cache */
  175.     return code;
  176. }
  177.  
  178. /*
  179.  * Swap the contents of a level dictionary (level2dict or ll3dict) and
  180.  * systemdict.  If a value in the level dictionary is itself a dictionary,
  181.  * and it contains a key/value pair referring to itself, swap its contents
  182.  * with the contents of the same dictionary in systemdict.  (This is a hack
  183.  * to swap the contents of statusdict.)
  184.  */
  185. private int
  186. swap_level_dict(i_ctx_t *i_ctx_p, const char *dict_name)
  187. {
  188.     ref *pleveldict;
  189.     ref rleveldict;
  190.     int index;
  191.     ref elt[2];            /* key, value */
  192.     ref *psubdict;
  193.  
  194.     /*
  195.      * We have to copy the refs for leveldict and subdict, since they may
  196.      * move if their containing dictionary is resized.
  197.      */
  198.     if (dict_find_string(systemdict, dict_name, &pleveldict) <= 0)
  199.     return_error(e_undefined);
  200.     rleveldict = *pleveldict;
  201.     index = dict_first(&rleveldict);
  202.     while ((index = dict_next(&rleveldict, index, &elt[0])) >= 0)
  203.     if (r_has_type(&elt[1], t_dictionary) &&
  204.         dict_find(&elt[1], &elt[0], &psubdict) > 0 &&
  205.         obj_eq(&elt[1], psubdict)
  206.         ) {
  207.         /* elt[1] is the 2nd-level sub-dictionary */
  208.         int isub = dict_first(&elt[1]);
  209.         ref subelt[2];
  210.         int found = dict_find(systemdict, &elt[0], &psubdict);
  211.         ref rsubdict;
  212.  
  213.         if (found <= 0)
  214.         continue;
  215.         rsubdict = *psubdict;
  216.         while ((isub = dict_next(&elt[1], isub, &subelt[0])) >= 0)
  217.         if (!obj_eq(&subelt[0], &elt[0])) {
  218.             /* don't swap dict itself */
  219.             int code = swap_entry(i_ctx_p, subelt, &rsubdict, &elt[1]);
  220.  
  221.             if (code < 0)
  222.             return code;
  223.         }
  224.     } else {
  225.         int code = swap_entry(i_ctx_p, elt, systemdict, &rleveldict);
  226.  
  227.         if (code < 0)
  228.         return code;
  229.     }
  230.     return 0;
  231. }
  232.  
  233. /*
  234.  * Swap an entry from a higher level dictionary into a base dictionary.
  235.  * elt[0] is the key, elt[1] is the current value in the Level 2 dictionary
  236.  * (*pdict2).
  237.  */
  238. private int
  239. swap_entry(i_ctx_t *i_ctx_p, ref elt[2], ref * pdict, ref * pdict2)
  240. {
  241.     ref *pvalue;
  242.     ref old_value;        /* current value in *pdict */
  243.     int found = dict_find(pdict, &elt[0], &pvalue);
  244.  
  245.     switch (found) {
  246.     default:        /* <0, error */
  247.         /*
  248.          * The only possible error here is a dictfull error, which is
  249.          * harmless.
  250.          */
  251.         /* fall through */
  252.     case 0:        /* missing */
  253.         make_null(&old_value);
  254.         break;
  255.     case 1:        /* present */
  256.         old_value = *pvalue;
  257.     }
  258.     /*
  259.      * Temporarily flag the dictionaries as local, so that we don't
  260.      * get invalidaccess errors.  (We know that they are both
  261.      * referenced from systemdict, so they are allowed to reference
  262.      * local objects even if they are global.)
  263.      */
  264.     {
  265.     uint space2 = r_space(pdict2);
  266.     int code;
  267.  
  268.     r_set_space(pdict2, avm_local);
  269.     idict_put(pdict2, &elt[0], &old_value);
  270.     if (r_has_type(&elt[1], t_null)) {
  271.         code = idict_undef(pdict, &elt[0]);
  272.         if (code == e_undefined &&
  273.         r_has_type(&old_value, t_null)
  274.         )
  275.         code = 0;
  276.     } else {
  277.         uint space = r_space(pdict);
  278.  
  279.         r_set_space(pdict, avm_local);
  280.         code = idict_put(pdict, &elt[0], &elt[1]);
  281.         r_set_space(pdict, space);
  282.     }
  283.     r_set_space(pdict2, space2);
  284.     return code;
  285.     }
  286. }
  287.